package com.dog.modules.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dog.framework.core.model.PageResult;
import com.dog.framework.mybatis.service.impl.BaseServiceImpl;
import com.dog.modules.system.convert.SysPostConvert;
import com.dog.modules.system.domain.SysPost;
import com.dog.modules.system.api.query.SysPostQuery;
import com.dog.modules.system.api.vo.SysPostVO;
import com.dog.modules.system.mapper.SysPostMapper;
import com.dog.modules.system.service.SysPostService;
import com.dog.modules.system.service.SysUserPostService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liuxk
 * @description 针对表【sys_post(岗位管理)】的数据库操作Service实现
 * @createDate 2024-01-30 14:47:29
 */
@Service
@AllArgsConstructor
public class SysPostServiceImpl extends BaseServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    private final SysUserPostService sysUserPostService;

    @Override
    public PageResult<SysPostVO> page(SysPostQuery query) {
        IPage<SysPost> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(SysPostConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private Wrapper<SysPost> getWrapper(SysPostQuery query) {
        LambdaQueryWrapper<SysPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(query.getPostCode()), SysPost::getPostCode, query.getPostCode());
        wrapper.like(StrUtil.isNotBlank(query.getPostName()), SysPost::getPostName, query.getPostName());
        wrapper.eq(query.getStatus() != null, SysPost::getStatus, query.getStatus());
        wrapper.orderByAsc(SysPost::getSort);
        return wrapper;
    }

    @Override
    public List<SysPostVO> getList() {
        SysPostQuery query = new SysPostQuery();
        //正常岗位列表
        query.setStatus(1);
        List<SysPost> entityList = baseMapper.selectList(getWrapper(query));

        return SysPostConvert.INSTANCE.convertList(entityList);
    }

    @Override
    public void save(SysPostVO vo) {
        SysPost entity = SysPostConvert.INSTANCE.convert(vo);
        baseMapper.insert(entity);
    }

    @Override
    public void update(SysPostVO vo) {
        SysPost entity = SysPostConvert.INSTANCE.convert(vo);
        updateById(entity);
    }

    @Override
    public void delete(List<Long> idList) {
        // 删除岗位
        removeByIds(idList);
        // 删除岗位用户关系
        sysUserPostService.deleteByPostIdList(idList);
    }
}




